<?php
/**
 * Created by PhpStorm.
 * User: xiaotao
 * Date: 18-5-3
 * Time: 下午5:20
 *
 */

namespace app\manage\controller;
use app\common\model\Article as Model;
use app\common\validate\Article as Validate;
use app\common\model\Category;
use think\Request;

class Article  extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $fields = ['id', 'title', 'category_id', 'group', 'keyword', 'author', 'source', 'abstract', 'favor', 'status'];
        $list = Model::field($fields)->order('id desc')->select();
        $count = count($list);
        $data = [
            'items' => $list ?: [],
            'total' => $count
        ];
        return $this->result_json(null, 1, $data);
    }

    /**
     * 于文章相关的配置项的获取
     */
    public function create(){
        $fields = ['id', 'name', 'pid'];
        $categories = Category::where('status', 1)
            ->order(['sort'])
            ->field($fields)->select();
        $list = Base::ListTree($categories->toArray());
        return $this->result_json(null, 1, $list);
    }


    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = $request->only(['title', 'banner', 'category', 'abstract', 'keyword', 'content', 'source', 'display_time', 'comment_disabled', 'status']);
        $data['category_id'] = $data['category'];
        $data['author'] = $this->baseInfo['uid'];
        $data['source'] = $data['source'] ?: $this->baseInfo['uname'];
        $data['comment_disabled'] = $data['comment_disabled'] === 'true' ? 1 : 0;
        $data['status'] = $data['status'] == 'draft' ? 2 : 1;

        $temp = Category::where('id', $data['category'])->value('group');
        if (is_null($temp)) {
            return $this->result_json('category error', 201);
        }
        $data['group'] = $temp;
        unset($data['category']);
        $validate = new Validate();
        if (!$validate->scene('New')->check($data)) {
            return $this->result_json($validate->getError(), 201);
        }
        $instance = new Model();
        if ($instance->save($data)) {
            return $this->result_json('create success', 1, ['id' => $instance->id]);
        } else {
            return $this->result_json('db error', 101);
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        // 字段验证和合法性验证
        $data = $request->only(['title', 'banner', 'category', 'abstract', 'keyword', 'content', 'source', 'display_time', 'comment_disabled', 'status']);
        $data['category_id'] = $data['category'];
        $data['author'] = $this->baseInfo['uid'];
        $data['source'] = $data['source'] ?: $this->baseInfo['uname'];
        $data['comment_disabled'] = $data['comment_disabled'] === 'true' ? 1 : 0;
        $data['status'] = $data['status'] == 'draft' ? 2 : 1;
        unset($data['category']);

        $validate = new Validate();
        if (!$validate->scene('Update')->check($data)) {
            return $this->result_json($validate->getError(), 201);
        }

        $instance = Model::get($id);    // 是否只查询单个字段
        if ($instance) {
            if ($instance->save($data) === false) {
                return $this->result_json('db error', 101);
            }
            return $this->result_json('update success', 1);
        } else {
            return $this->result_json('param error', 201);
        }
    }


    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id, $flag = 'true')
    {
        $instance = Model::where('id', $id)->find();
        if (!$instance) {
            return $this->result_json('param error', 201);
        }

        $instance->status = $flag === 'true' ? -1 : 1;
        // 执行删除操作
        if ($instance->save() === false) {
            return $this->result_json('param error', 201);
        }
        return $this->result_json('delete success', 0);
    }


}